When encoding a sequence of video frames, it is common practice to divide each video frame into blocks of pixels, such as blocks of 16×16 pixels, 32×32 pixels, or 64×64 pixels depending on the coding standard used, and encode the frame block-by-block. These blocks are known as macroblocks in H.264/MPEG-4 AVC, and coding units in H.265/HEVC. The blocks may be encoded as intra-blocks, sometimes referred to as I-blocks, or as inter-blocks, sometimes referred to as P- or B-blocks. If a block is intra-coded, the pixel values are encoded with reference to the current frame only, such as by extrapolating from the edges of neighbouring blocks in the same frame. This is in contrast to inter-coded blocks, which are encoded with reference to a block of pixels in a reference frame by carrying out motion compensation. The motion compensation includes finding a matching block of pixels in the reference frame. In order to find the matching block, a motion vector and a sum of difference (SAD) value may be evaluated with respect to a plurality of blocks of pixels in the reference frame. The block of pixels in the reference frame that gives the best match according to a cost function, which is a function of the motion vector and the SAD value, is then selected.
In some frames, referred to as intra-frames, all blocks of pixels are intra-coded. In other frames, referred to as inter-frames, some blocks of pixels are intra-coded and others are inter-coded. In inter-frames, a decision is taken whether to encode each block of pixels as an intra-coded block or an inter-coded block. The decision is usually taken based on which of intra-coding and inter-coding will give the lowest encoding cost. The cost for intra-coding a block is typically given in terms of a predefined value corresponding to an average cost of intra-coding a block as determined from historical data. The cost for inter-coding a block is typically determined as a weighted sum of the cost of encoding the motion vector and the SAD value of the selected block in the reference frame, for some predefined weights.
The above approach of using predefined values for calculating the costs for intra-coding, and predefined weights for calculating the costs for inter-coding may however lead to suboptimal performance in some cases. For example, consider a block of pixels having low contrast, such as a block depicting a clear sky or a white wall. The above approach will be biased towards selecting to encode the block as an inter-coded block, since it typically will be possible to find plenty of matching blocks in the reference frame having low SAD values. Further, for the same reason, there will be a bias towards selecting a zero motion vector. This may be disadvantageous in many situations. For example, this may lead to that the motion of a small moving object, such as a distant airplane in a clear sky, or a line on a wall which moves in the video as the camera sweeps by, is not correctly encoded. Rather, the motion of the object will have a jumping behaviour in the decoded video. This may be detrimental in a surveillance situation where also a small detail in an otherwise uniform background may be important. There is thus room for improvements.